package org.zcl.enums;

import com.google.common.collect.ImmutableMap;
import lombok.Getter;
import org.zcl.service.impl.QueryAsyncServiceImpl;
import org.zcl.utils.QueryService;
import org.zcl.service.impl.QueryMainServiceImpl;
import org.zcl.service.impl.QueryServiceImpl;
import org.zcl.utils.SpringBeanUtils;

import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Getter
public enum StrategyEnum {


    MAIN_CHANNEL(1, SpringBeanUtils.getBean(QueryMainServiceImpl.class), "主频道"),
    MAIN_CHANNEL2(2, SpringBeanUtils.getBean(QueryAsyncServiceImpl.class), "主频道2"),
    PACKAGE_ACTIVITY_CHANNEL(3, SpringBeanUtils.getBean(QueryServiceImpl.class), "礼包活动频道"),
    ;
    private final Integer type;
    private final QueryService queryService;
    private final String desc;
    private static final Map<Integer, StrategyEnum> map = ImmutableMap.copyOf(Stream.of(StrategyEnum.values()).collect(Collectors.toMap(StrategyEnum::getType, Function.identity())));

    StrategyEnum(Integer type, QueryService queryService, String desc) {
        this.type = type;
        this.queryService = queryService;
        this.desc = desc;
    }

    public static String getDesc(Integer type) {
        StrategyEnum s = map.get(type);
        return s == null ? "未知" : s.getDesc();
    }

    public static QueryService getAdapter(Integer type) {
        StrategyEnum s = map.get(type);
        if (Objects.isNull(s)) {
            return SpringBeanUtils.getBean(QueryService.class);
        }
        return s.getQueryService();
    }
}
